package com.wolfking.converter.dropdown;

import com.google.common.collect.Maps;
import com.wolfking.jeesite.entity.Dict;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

@Slf4j
public class DictValueLabelParameterDropDown implements ParameterDropDown {

    private final String type;

    public DictValueLabelParameterDropDown(String type) {
        this.type = type;
    }

    @Override
    public Map<String, String> buildParameterDropDown() {
        try {
            if (StringUtils.isNotBlank(type)) {
                Map<String, String> collect = DICT_CACHE.get(type).stream().collect(Collectors.toMap(Dict::getValue,
                        (d) -> String.format("%s[%s]", d.getValue(), d.getLabel()), (e1, e2) -> e1));
                return new TreeMap<>(collect);
            }
        } catch (Exception e) {
            log.warn("", e);
        }
        return Maps.newHashMap();
    }
}
